package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.ReportGxlqTxhfjlbBsInfo;
import com.ruoyi.report_lq.domain.ReportRhlqPrsdsyBaDetail;
import com.ruoyi.report_lq.domain.bo.ReportGxlqTxhfjlbBsInfoBo;
import com.ruoyi.report_lq.domain.vo.ReportRhlqPrsdsyBaDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportGxlqTxhfjlbBaDetailBo;
import com.ruoyi.report_lq.domain.vo.ReportGxlqTxhfjlbBaDetailVo;
import com.ruoyi.report_lq.domain.ReportGxlqTxhfjlbBaDetail;
import com.ruoyi.report_lq.mapper.ReportGxlqTxhfjlbBaDetailMapper;
import com.ruoyi.report_lq.service.IReportGxlqTxhfjlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2024-07-03
 */
@RequiredArgsConstructor
@Service
public class ReportGxlqTxhfjlbBaDetailServiceImpl implements IReportGxlqTxhfjlbBaDetailService {

    private final ReportGxlqTxhfjlbBaDetailMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public ReportGxlqTxhfjlbBaDetailVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }


    public ReportGxlqTxhfjlbBaDetailVo queryVoByInfoId (Long infoId) {
        QueryWrapper<ReportGxlqTxhfjlbBaDetail> query = new QueryWrapper<>();
        query.eq("common_id", infoId);
        ReportGxlqTxhfjlbBaDetailVo reVo = baseMapper.selectVoOne(query);
        if(BeanUtil.isEmpty(reVo)) {
            return new ReportGxlqTxhfjlbBaDetailVo();
        }
        return baseMapper.selectVoOne(query);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ReportGxlqTxhfjlbBaDetailVo> queryPageList(ReportGxlqTxhfjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportGxlqTxhfjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportGxlqTxhfjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ReportGxlqTxhfjlbBaDetailVo> queryList(ReportGxlqTxhfjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportGxlqTxhfjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportGxlqTxhfjlbBaDetail> buildQueryWrapper(ReportGxlqTxhfjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportGxlqTxhfjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommonId() != null, ReportGxlqTxhfjlbBaDetail::getCommonId, bo.getCommonId());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbzrd()), ReportGxlqTxhfjlbBaDetail::getJiszbzrd, bo.getJiszbzrd());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgzrd()), ReportGxlqTxhfjlbBaDetail::getShicjgzrd, bo.getShicjgzrd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdzrd()), ReportGxlqTxhfjlbBaDetail::getJiegpdzrd, bo.getJiegpdzrd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbzrdpi()), ReportGxlqTxhfjlbBaDetail::getJiszbzrdpi, bo.getJiszbzrdpi());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgzrdpi()), ReportGxlqTxhfjlbBaDetail::getShicjgzrdpi, bo.getShicjgzrdpi());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdzrdpi()), ReportGxlqTxhfjlbBaDetail::getJiegpdzrdpi, bo.getJiegpdzrdpi());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbyd()), ReportGxlqTxhfjlbBaDetail::getJiszbyd, bo.getJiszbyd());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgyd()), ReportGxlqTxhfjlbBaDetail::getShicjgyd, bo.getShicjgyd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdyd()), ReportGxlqTxhfjlbBaDetail::getJiegpdyd, bo.getJiegpdyd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbrhd()), ReportGxlqTxhfjlbBaDetail::getJiszbrhd, bo.getJiszbrhd());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgrhd()), ReportGxlqTxhfjlbBaDetail::getShicjgrhd, bo.getShicjgrhd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdrhd()), ReportGxlqTxhfjlbBaDetail::getJiegpdrhd, bo.getJiegpdrhd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbydydnd()), ReportGxlqTxhfjlbBaDetail::getJiszbydydnd, bo.getJiszbydydnd());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgydnd()), ReportGxlqTxhfjlbBaDetail::getShicjgydnd, bo.getShicjgydnd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdydnd()), ReportGxlqTxhfjlbBaDetail::getJiegpdydnd, bo.getJiegpdydnd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbsd()), ReportGxlqTxhfjlbBaDetail::getJiszbsd, bo.getJiszbsd());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgsd()), ReportGxlqTxhfjlbBaDetail::getShicjgsd, bo.getShicjgsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdsd()), ReportGxlqTxhfjlbBaDetail::getJiegpdsd, bo.getJiegpdsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbrjd()), ReportGxlqTxhfjlbBaDetail::getJiszbrjd, bo.getJiszbrjd());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgrjd()), ReportGxlqTxhfjlbBaDetail::getShicjgrjd, bo.getShicjgrjd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdrjd()), ReportGxlqTxhfjlbBaDetail::getJiegpdrjd, bo.getJiegpdrjd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbtxhf()), ReportGxlqTxhfjlbBaDetail::getJiszbtxhf, bo.getJiszbtxhf());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgtxhf()), ReportGxlqTxhfjlbBaDetail::getShicjgtxhf, bo.getShicjgtxhf());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdtxhf()), ReportGxlqTxhfjlbBaDetail::getJiegpdtxhf, bo.getJiegpdtxhf());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbnrx()), ReportGxlqTxhfjlbBaDetail::getJiszbnrx, bo.getJiszbnrx());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgnrx()), ReportGxlqTxhfjlbBaDetail::getShicjgnrx, bo.getShicjgnrx());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdnrx()), ReportGxlqTxhfjlbBaDetail::getJiegpdnrx, bo.getJiegpdnrx());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbrx()), ReportGxlqTxhfjlbBaDetail::getJiszbrx, bo.getJiszbrx());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgrx()), ReportGxlqTxhfjlbBaDetail::getShicjgrx, bo.getShicjgrx());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdrx()), ReportGxlqTxhfjlbBaDetail::getJiegpdrx, bo.getJiegpdrx());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbzcwdxlx()), ReportGxlqTxhfjlbBaDetail::getJiszbzcwdxlx, bo.getJiszbzcwdxlx());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgzcwdxlx()), ReportGxlqTxhfjlbBaDetail::getShicjgzcwdxlx, bo.getShicjgzcwdxlx());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdzcwdxlx()), ReportGxlqTxhfjlbBaDetail::getJiegpdzcwdxlx, bo.getJiegpdzcwdxlx());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbzlbh()), ReportGxlqTxhfjlbBaDetail::getJiszbzlbh, bo.getJiszbzlbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgzlbh()), ReportGxlqTxhfjlbBaDetail::getShicjgzlbh, bo.getShicjgzlbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdzlbh()), ReportGxlqTxhfjlbBaDetail::getJiegpdzlbh, bo.getJiegpdzlbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbzrdb()), ReportGxlqTxhfjlbBaDetail::getJiszbzrdb, bo.getJiszbzrdb());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgzrdb()), ReportGxlqTxhfjlbBaDetail::getShicjgzrdb, bo.getShicjgzrdb());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdzrdb()), ReportGxlqTxhfjlbBaDetail::getJiegpdzrdb, bo.getJiegpdzrdb());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszbyd5()), ReportGxlqTxhfjlbBaDetail::getJiszbyd5, bo.getJiszbyd5());
        lqw.eq(StringUtils.isNotBlank(bo.getShicjgyd5()), ReportGxlqTxhfjlbBaDetail::getShicjgyd5, bo.getShicjgyd5());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpdyd5()), ReportGxlqTxhfjlbBaDetail::getJiegpdyd5, bo.getJiegpdyd5());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ReportGxlqTxhfjlbBaDetailBo bo) {
        ReportGxlqTxhfjlbBaDetail add = BeanUtil.toBean(bo, ReportGxlqTxhfjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ReportGxlqTxhfjlbBaDetailBo bo) {
        ReportGxlqTxhfjlbBaDetail update = BeanUtil.toBean(bo, ReportGxlqTxhfjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 修改乳化沥青破乳速度记录
     */
    public void insertOrUpdate(ReportGxlqTxhfjlbBaDetailBo bo, Long pId) {
        ReportGxlqTxhfjlbBaDetail detail = BeanUtil.toBean(bo, ReportGxlqTxhfjlbBaDetail.class);
        detail.setCommonId(pId);
        baseMapper.insertOrUpdate(detail);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportGxlqTxhfjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
